<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="orderPerson">
        <el-input
          v-model="queryParams.orderPerson"
          placeholder="请输入制单人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable style="width: 200px;">
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['medicine:order:add']"
        >新增订单</el-button>
      </el-col>
      <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleOrderCart"
            v-hasPermi="['medicine:drug:add']"
          >我的采购</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['medicine:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['medicine:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['medicine:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="订单编号" align="center" prop="orderNum">
        <template #default="scope">
               <router-link :to="'/medicine/detail/orderDetail/'+scope.row.orderId" class="link-type">
                  <span>{{ scope.row.orderNum }}</span>
               </router-link>
            </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="user.userName" />
      <el-table-column label="供应商" align="center" prop="supplier.supplierName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link v-if="scope.row.orderStatus=='2'" type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:order:edit']">修改</el-button>
          <el-button link v-if="scope.row.orderStatus=='1'" type="primary" icon="add" @click="handleOrderBuy(scope.row)" v-hasPermi="['medicine:order:edit']">点击采购</el-button>
          <el-button link v-if="scope.row.orderStatus=='10'" type="primary" icon="Edit" @click="handleOrderStatus(scope.row)" v-hasPermi="['medicine:order:edit']">下一步</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in buy_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/his/order";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { order_status,buy_status } = proxy.useDict('order_status','buy_status');

const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNum: null,
    orderPerson: null,
    supplierId: null,
    orderStatus: null,
  },
  rules: {
    supplierId: [
      { required: true, message: "供应商ID不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);


/** 查询采购管理列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderNum: null,
    orderPerson: null,
    supplierId: null,
    orderMoney: null,
    orderStatus: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  router.push('/medicine/orderAdd/addOrder')
}

/** 修改按钮操作 */
function handleUpdate(row) {
 router.push('/medicine/update/updateOrder/'+row.orderId)
}



/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value;
  proxy.$modal.confirm('是否确认删除采购管理编号为"' + _orderIds + '"的数据项？').then(function() {
    return delOrder(_orderIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('medicine/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}
 //查看我的采购
 function handleOrderCart(){
    router.push('/medicine/cart/cartInfo')
  }
  //采购
function handleOrderBuy(row){
  const data ={
    orderId:row.orderId,
    orderStatus:10
  }
  proxy.$modal.confirm('是否确认采购编号为"' + row.orderNum + '"的数据项？').then(function() {
    return updateOrder(data);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("开始采购");
  }).catch(() => {});
}
//采购之后,下一步
function handleOrderStatus(row){
  form.value.orderId=row.orderId
  open.value=true;
  title.value="修改采购状态"
}
/** 提交按钮 */
function submitForm() {
      if (form.value.orderId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("提交成功");
          open.value = false;
          getList();
        });
    }
}
getList();
</script>
